# coding: utf-8
# @Author : lryself
# @Date : 2020/12/13 20:39
# @Software: PyCharm
from flask import jsonify, g
from flask_restplus import Resource

from . import main_rest_api, back_model
from ..service.userCollectManager import UserCollectManager

from ..service.informationInfoManager import InformationInfoManager

collect_post_parser = main_rest_api.parser()
collect_post_parser.add_argument('informationID', type=str, location='form', required=True)
collect_post_parser.add_argument('title', type=str, location='form', required=True)

collect_delete_parser = main_rest_api.parser()
collect_delete_parser.add_argument('informationID', type=str, location='form', required=True)

collect_get_parser = main_rest_api.parser()
collect_get_parser.add_argument('informationID', type=str, location='args')


class CollectResource(Resource):
    @main_rest_api.doc(description="添加收藏")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(collect_post_parser)
    def post(self):
        user_id = g.user.user_id
        kwargs = collect_post_parser.parse_args()

        args = {"information_id": kwargs.get("informationID"), "title": kwargs.get("title"), "user_id": user_id, "type": 3}
        result_dict = UserCollectManager.add(**args)

        return result_dict

    @main_rest_api.doc(description="取消收藏")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(collect_delete_parser)
    def delete(self):
        user_id = g.user.user_id
        kwargs = collect_delete_parser.parse_args()

        args = {"information_id": kwargs.get("informationID"), "user_id": user_id, "type": 3}

        result_dict = UserCollectManager.delete(**args)

        return result_dict

    @main_rest_api.doc(description="获取收藏列表")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(collect_get_parser)
    def get(self):
        kwargs = collect_list_get_parser.parse_args()

        args = {"InformationID": kwargs.get("informationID")}
        result_dict = InformationInfoManager.get(**args)

        return result_dict

collect_list_get_parser = main_rest_api.parser()
collect_list_get_parser.add_argument('informationID', type=str, location='args')
collect_list_get_parser.add_argument('collectID', type=str, location='args')
collect_list_get_parser.add_argument('collectType', type=str, location='args')


class CollectListResource(Resource):
    @main_rest_api.doc(description="获取收藏的资料内容")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(collect_list_get_parser)
    def get(self):
        kwargs = collect_list_get_parser.parse_args()
        args = {
            "information_id": kwargs.get("informationID"),
            "collect_id": kwargs.get("collectID"),
            "collect_type": kwargs.get("collectType")
        }
        result_dict = UserCollectManager.get(**args)

        return result_dict